
#ifndef RTHTTPMOZILLAPROFILE_H
#define RTHTTPMOZILLAPROFILE_H

#include "RtStdCpp.h"
#include "RtHttpProxySetting.h"

typedef INT32 REGOFF;   /* offset into registry file */
typedef INT32 RKEY;

typedef struct _hdr
{
    DWORD	magic;      /* must equal MAGIC_NUMBER */
    WORD	verMajor;   /* major version number */
    WORD	verMinor;   /* minor version number */
    REGOFF	avail;      /* next available offset */
    REGOFF	root;       /* root object */
} REGHDR;

typedef struct _stdnodes {
    REGOFF	versions;
    REGOFF	users;
    REGOFF	common;
    REGOFF	current_user;
    REGOFF	privarea;
} STDNODES;

typedef struct _regfile
{
	FILE           *fh;
    REGHDR          hdr;
    int             refCount;
    int             hdrDirty;
    int             inInit;
    int             readOnly;
    char *          filename;
    STDNODES        rkeys;
} REGFILE;

class CRtHttpMozillaProfile
{
public:
	CRtHttpMozillaProfile(IRtHttpProxySetting::BrowseType aBrowserType);
	~CRtHttpMozillaProfile();

	BOOL GetCurrentProfileName(CRtString &aRetval);

	BOOL OpenWithoutRegName();
	BOOL GetKey(RKEY aBaseKey, LPCSTR aKeyname, RKEY &aRetval);
	BOOL GetString(RKEY aBaseKey, LPCSTR aValname, CRtString &aRetval);

private:
	IRtHttpProxySetting::BrowseType m_BrowserType;
	REGFILE m_newRegFile;

	BOOL OpenRegFile(const CRtString &aFileName);
	BOOL GetCurrentProfileDirectory(CRtString &aRetval);
	BOOL GetNativeRegName(CRtString &aRetval, BOOL aIncludeName = TRUE);
	BOOL Get4xNativeRegName(CRtString &aRetval);
};

/* Standard keys */
#define ROOTKEY_USERS                   (0x01)
#define ROOTKEY_COMMON                  (0x02)
#define ROOTKEY_CURRENT_USER            (0x03)
#define ROOTKEY_PRIVATE                 (0x04)
#define ROOTKEY                         (0x20)
#define ROOTKEY_VERSIONS                (0x21)

/* strings for standard keys */
#define ROOTKEY_STR             "/"
#define ROOTKEY_VERSIONS_STR    "Version Registry"
#define ROOTKEY_USERS_STR       "Users"
#define ROOTKEY_COMMON_STR      "Common"
#define ROOTKEY_PRIVATE_STR     "Private Arenas"

/* Total path length */
#define MAXREGPATHLEN   (2048)
/* Name on the path (including null terminator) */
#define MAXREGNAMELEN   (512)
/* Value of an entry */
#define MAXREGVALUELEN  (0x7FFF)

#define MAGIC_NUMBER    0x76644441L
#define MAJOR_VERSION   1          /* major version for incompatible changes */
#define MINOR_VERSION   2          /* minor ver for new (compatible) features */
#define HDRRESERVE      128        /* number of bytes reserved for hdr */

#define REGTYPE_ENTRY             (0x0010)
#define REGTYPE_ENTRY_STRING_UTF  (REGTYPE_ENTRY + 1)
#define TYPE_IS_ENTRY(type)       ( (type) & REGTYPE_ENTRY )
#define TYPE_IS_KEY(type)         ( !((type) & REGTYPE_ENTRY) )

#define REGTYPE_KEY                   (1)
#define REGTYPE_DELETED               (0x0080)

typedef struct _desc
{
    REGOFF  location;   /* this object's offset (for verification) */
    REGOFF  name;       /* name CRtString */
    WORD    namelen;    /* length of name CRtString (including terminator) */
    WORD    type;       /* node type (key, or entry style) */
    REGOFF  left;       /* next object at this level (0 if none) */
    REGOFF  down;       /* KEY: first subkey        VALUE: 0 */
    REGOFF  value;      /* KEY: first entry object  VALUE: value CRtString */
    DWORD   valuelen;   /* KEY: 0  VALUE: length of value data */
    DWORD   valuebuf;   /* KEY: 0  VALUE: length available */
    REGOFF  parent;     /* the node on the immediate level above */
} REGDESC;

/* offsets into structure on disk */
#define DESC_LOCATION   0
#define DESC_NAME       4
#define DESC_NAMELEN    8
#define DESC_TYPE       10
#define DESC_LEFT       12
#define DESC_DOWN       16
#define DESC_VALUE      20
#define DESC_VALUELEN   24
#define DESC_VALUEBUF   16    /* stored in place of "down" for entries */
#define DESC_PARENT     28
#define DESC_SIZE       32    /* size of desc on disk */

#define HDR_MAGIC       0
#define HDR_VERMAJOR    4
#define HDR_VERMINOR    6
#define HDR_AVAIL       8
#define HDR_ROOT        12

#endif // !RTHTTPMOZILLAPROFILE_H
